﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using equinoxWeb.Models;
using equinoxWeb.GestionConexion;
using equinoxWeb.SearchObjects;
using System.Web.UI;
using System.Web.Script.Serialization;

namespace equinoxWeb.Controllers
{
    public class ClienteController : EquinoxController
    {
        //
        // GET: /Cliente/

        public ActionResult Index()
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            return View();
        }

        //
        // GET: /Cliente/Details/5

        public ActionResult Details(int Idcliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
            {
                if (idClienteLogueado != Idcliente)
                    return View("~/Views/Shared/PermisoDenegado.cshtml");
            }
            ViewBag.Cliente = ClienteFunction.abrirCliente(Idcliente);
            return View(ClienteFunction.abrirCliente(Idcliente));
        }

        //
        // GET: /Cliente/Create

        public ActionResult Create()
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            return View();
        }

        //
        // POST: /Cliente/Create

        [HttpPost]
        public ActionResult Create(Cliente cliente)
        {
            try
            {
                if (defaultAction != null) return defaultAction;
                if (esCliente)
                    return View("~/Views/Shared/PermisoDenegado.cshtml");

                ClienteFunction.agregarCliente(cliente,this);
                RouteValueDictionary routeValues = new RouteValueDictionary();
                routeValues.Add("Idcliente", cliente.Idcliente);
                return RedirectToAction("Details", routeValues);
            }
            catch (Exception ex)
            {
                ViewBag.error = mensajeError == null || mensajeError == "" ? ex.Message : mensajeError;
                defaultAction = View("~/Views/Shared/Error.cshtml");
                return defaultAction;
            }
        }

        //
        // GET: /Cliente/Edit/5

        public ActionResult Edit(int idCliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            Cliente cliente = ClienteFunction.abrirCliente(idCliente);
            return View(cliente);
        }

        //
        // POST: /Cliente/Edit/5

        [HttpPost]
        public ActionResult Edit(Cliente cliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            try
            {
                ClienteFunction.modificarCliente(cliente);
                // TODO: Add update logic here
                RouteValueDictionary routeValues = new RouteValueDictionary();
                routeValues.Add("Idcliente", cliente.Idcliente);
                return RedirectToAction("Details", routeValues);
            }
            catch
            {
                return View(cliente);
            }
        }

        //
        // GET: /Cliente/Delete/5

        public ActionResult Delete(int id)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            return View();
        }

        //
        // POST: /Cliente/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Cliente/Search/6

        public ActionResult Search()
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            return View();
        }

        //
        // GET: /Cliente/SearchResults/7

        public ActionResult SearchResults()
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            List<Cliente> clienteResults = ClienteFunction.buscarCliente(new ClienteBusqueda());
            ViewBag.clienteResults = clienteResults;
            return View();
        }

        //
        // POST: /Cliente/SearchResults/7

        [HttpPost]
        public ActionResult SearchResults(ClienteBusqueda cliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            List<Cliente> clienteResults = ClienteFunction.buscarCliente(cliente);
            ViewBag.clienteResults = clienteResults;
            return View();
        }

        public ActionResult AddClub(int idCliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            List<Club> clubResults = ClubFunction.listarClubes(idCliente);
            ViewBag.idCliente = idCliente;
            ViewBag.clubResults = clubResults;
            return View();
        }

        [HttpPost]
        public ActionResult AddClub(string [] nombre,string[] direccion, int idCliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            ClubFunction.editarClubes(idCliente,nombre,direccion);

            RouteValueDictionary routeValues = new RouteValueDictionary();
            routeValues.Add("Idcliente", idCliente);
            return RedirectToAction("ListClub", routeValues);
        }

        public ActionResult ListClub(int idCliente)
        {
            if (defaultAction != null) return defaultAction;
            if (esCliente)
                return View("~/Views/Shared/PermisoDenegado.cshtml");

            List<Club> clubResults = ClubFunction.listarClubes(idCliente);
            ViewBag.clubResults = clubResults;
            ViewBag.idCliente = idCliente;
            return View();
        }

        [HttpPost]
        public JsonResult ObtenerClub(int idCliente)
        {
            List<Club> clubResults = ClubFunction.listarClubes(idCliente);
            ViewBag.clubResults = clubResults;
            ViewBag.idCliente = idCliente;

            var serializer = new JavaScriptSerializer();
            var serializedResult = serializer.Serialize(clubResults);
            ViewBag.data = serializedResult;
            return Json(clubResults);
        }

    }
}
